import CommonController from '../abstract/controller/common';
import { roleAuthority } from '../abstract/controller/decorators';
import { JenkinsTaskJobItem } from '../model/jenkinsTaskModel';
import ResponseModel, { PageResponseModel } from '../model/responseModel';
import { UserRoleEnum } from "@/constants/enum.types";
export default class JenkinsController extends CommonController {
  /** 根据api名称和参数调用jenkins api */
  public async common() {
    const { ctx } = this;
    const { body } = ctx.request;
    /** 参数校验 */
    ctx.validate({
      apiKey: { type: 'string' },
      options: { type: 'array' },
    });
    try {
      const res = await ctx.service.jenkinsService.common(body);
      ctx.body = new ResponseModel({
        data: res,
        message: '调用成功',
        success: true,
      });
    } catch (error) {
      ctx.body = new ResponseModel({
        // @ts-ignore
        message: error && error.toString(),
        success: false,
      });
    }
  }

  /** 多任务构建 */
  public async multipleBuilds() {
    const { ctx } = this;
    const { body } = ctx.request;
    const errors = this.app.validator.validate({
      jobList: { type: 'array' },
    }, body);
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    const jobList: JenkinsTaskJobItem[] = body.jobList;
    const data: any = [];
    for (const item of jobList) {
      try {
        await ctx.service.jenkinsService.common({
          apiKey: 'build_with_params',
          options: [
            item.name,
            item.params,
          ],
        });
        data.push({
          success: true,
          message: '指令发送成功',
          name: item.name,
          params: item.params,
        });
      } catch (error) {
        data.push({
          success: false,
          message: error,
          name: item.name,
          params: item.params,
        });
      }
    }
    ctx.body = new ResponseModel({
      data,
      message: '调用成功',
      success: true,
    });
  }

  /** 获取当前构建队列html */
  public async getBuildQueue() {
    this.ctx.body = new ResponseModel({
      data: await this.ctx.service.jenkinsService.getBuildQueue(),
      message: '调用成功',
      success: true,
    });
  }

  /** 获取当前构建执行状态html */
  public async getExecutors() {
    this.ctx.body = new ResponseModel({
      data: await this.ctx.service.jenkinsService.getExecutors(),
      message: '调用成功',
      success: true,
    });
  }

  /** 获取构建日志html */
  public async getPogressiveHtml() {
    const { body } = this.ctx.request;
    const errors = this.app.validator.validate({
      jobName: { type: 'string' },
      buildNumber: { type: 'string' },
      start: { type: 'number', required: false },
    }, body);
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    const { jobName, buildNumber, start } = body;
    this.ctx.body = new ResponseModel({
      data: await this.ctx.service.jenkinsService.getPogressiveHtml(jobName, buildNumber, start),
      message: '调用成功',
      success: true,
    });
  }

  /** 获取项目列表 */
  public async taskList() {
    const { ctx } = this;
    const { body } = ctx.request;
    const errors = this.app.validator.validate({
      pageIndex: { type: 'number' },
      pageSize: { type: 'number' },
      name: { type: 'string', required: false },
      createTime: { type: 'array', required: false },
      updateTime: { type: 'array', required: false },
    }, body);
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    try {
      const res = await this.ctx.service.jenkinsService.queryList({
        pageIndex: body.pageIndex as number,
        pageSize: body.pageSize as number,
        name: body.name || '',
        description: body.description || '',
        project: body.project || '',
        createTime: body.createTime || [],
        updateTime: body.updateTime || [],
      });
      this.ctx.body = new PageResponseModel({
        success: true,
        message: '查询成功',
        data: res,
      });
    } catch (error) {
      this.ctx.body = new PageResponseModel({
        success: false,
        // @ts-ignore
        message: error && error.toString(),
      });
    }
  }

  /** 根据id查询项目详情 */
  public async detail() {
    const { ctx } = this;
    const { body } = ctx.request;
    const errors = this.app.validator.validate({
      id: { type: 'string' },
    }, body);
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    try {
      const res = await this.ctx.service.jenkinsService.detail(body.id);
      this.ctx.body = new ResponseModel({
        success: true,
        data: res,
        message: '查询成功',
      });
    } catch (error) {
      this.ctx.body = new ResponseModel({
        success: false,
        // @ts-ignore
        message: error,
      });
    }
  }

  /** 根据id删除项目 */
  @roleAuthority(UserRoleEnum.dev_ops)
  public async delete() {
    const { ctx } = this;
    const { body } = ctx.request;
    const errors = this.app.validator.validate({
      id: { type: 'string' },
    }, body);
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    try {
      const res = await this.ctx.service.jenkinsService.delete(body.id);
      this.ctx.body = new ResponseModel({
        success: true,
        data: res,
        message: '删除成功',
      });
    } catch (error) {
      this.ctx.body = new ResponseModel({
        success: false,
        // @ts-ignore
        message: error,
      });
    }
  }

  /** 合并项目 */
  @roleAuthority(UserRoleEnum.dev_ops)
  public async merge() {
    const { ctx } = this;
    const { body } = ctx.request;
    const errors = this.app.validator.validate({
      idList: { type: 'array' },
    }, body);
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    try {
      const res = await this.ctx.service.jenkinsService.merge(body.idList);
      this.ctx.body = new ResponseModel({
        success: true,
        data: res,
        message: '合并成功',
      });
    } catch (error) {
      this.ctx.body = new ResponseModel({
        success: false,
        // @ts-ignore
        message: error,
      });
    }
  }

  /** 任务 创建或修改 */
  public async createOrModify() {
    const { ctx } = this;
    const { body } = ctx.request;
    /** 参数校验 */
    const errors = body.id
    ? this.app.validator.validate(
        {
          id: { type: 'string' },
          name: { type: 'string' },
          jobList: { type: 'array' },
        },
        body,
      )
    : this.app.validator.validate(
        {
          name: { type: 'string' },
          jobList: { type: 'array' },
        },
        body,
      );
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    try {
      const res = await this.ctx.service.jenkinsService.createOrModify(body);
      this.ctx.body = new ResponseModel({
        success: true,
        data: res,
        message: !body.id ? '新增成功' : '修改成功',
      });
    } catch (error) {
      this.ctx.body = new ResponseModel({
        success: false,
        // @ts-ignore
        message: error && error.toString(),
      });
    }
  }

  /** 获取计划列表 */
  public async planList() {
    const { ctx } = this;
    const { body } = ctx.request;
    const errors = this.app.validator.validate({
      pageIndex: { type: 'number' },
      pageSize: { type: 'number' },
      name: { type: 'string', required: false },
      status: { type: 'number', required: false },
      createTime: { type: 'array', required: false },
      updateTime: { type: 'array', required: false },
    }, body);
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    try {
      const res = await this.ctx.service.jenkinsService.queryPlanList({
        pageIndex: body.pageIndex as number,
        pageSize: body.pageSize as number,
        name: body.name || '',
        status: body.status || 0,
        executionTime: body.executionTime || [],
        createTime: body.createTime || [],
        updateTime: body.updateTime || [],
      });
      this.ctx.body = new PageResponseModel({
        success: true,
        message: '查询成功',
        data: res,
      });
    } catch (error) {
      this.ctx.body = new PageResponseModel({
        success: false,
        // @ts-ignore
        message: error && error.toString(),
      });
    }
  }

  /** 计划 创建或修改 */
  public async createOrModifyPlan() {
    const { ctx } = this;
    const { body } = ctx.request;
    /** 参数校验 */
    const errors = body.id
    ? this.app.validator.validate(
        {
          id: { type: 'string' },
          name: { type: 'string' },
          jobList: { type: 'array' },
        },
        body,
      )
    : this.app.validator.validate(
        {
          name: { type: 'string' },
          jobList: { type: 'array' },
        },
        body,
      );
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    try {
      const res = await this.ctx.service.jenkinsService.createOrModifyPlan(body);
      this.ctx.body = new ResponseModel({
        success: true,
        data: res,
        message: !body.id ? '新增成功' : '修改成功',
      });
    } catch (error) {
      this.ctx.body = new ResponseModel({
        success: false,
        // @ts-ignore
        message: error && error.toString(),
      });
    }
  }

  /** 根据id查询项目详情 */
  public async planDetail() {
    const { ctx } = this;
    const { body } = ctx.request;
    const errors = this.app.validator.validate({
      id: { type: 'string' },
    }, body);
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    try {
      const res = await this.ctx.service.jenkinsService.planDetail(body.id);
      this.ctx.body = new ResponseModel({
        success: true,
        data: res,
        message: '查询成功',
      });
    } catch (error) {
      this.ctx.body = new ResponseModel({
        success: false,
        // @ts-ignore
        message: error,
      });
    }
  }

  /** 根据id删除项目 */
  @roleAuthority(UserRoleEnum.dev_ops)
  public async planDelete() {
    const { ctx } = this;
    const { body } = ctx.request;
    const errors = this.app.validator.validate({
      id: { type: 'string' },
    }, body);
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    try {
      const res = await this.ctx.service.jenkinsService.planDelete(body.id);
      this.ctx.body = new ResponseModel({
        success: true,
        data: res,
        message: '删除成功',
      });
    } catch (error) {
      this.ctx.body = new ResponseModel({
        success: false,
        // @ts-ignore
        message: error,
      });
    }
  }

  /** 修改当前计划状态 */
  @roleAuthority(UserRoleEnum.dev_ops)
  public async changePlanStatus() {
    const { ctx } = this;
    const { body } = ctx.request;
    const errors = this.app.validator.validate({
      id: { type: 'string' },
      status: { type: 'number' },
    }, body);
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    try {
      const res = await this.ctx.service.jenkinsService.changePlanStatus(body.id, body.status);
      this.ctx.body = new ResponseModel({
        success: true,
        data: res,
        message: '修改成功',
      });
    } catch (error) {
      this.ctx.body = new ResponseModel({
        success: false,
        // @ts-ignore
        message: error,
      });
    }
  }
}
